/*
PREPERATION OF AGE 55+ DISCONTINUITY DATA

Data 	: A3_generate.dta"
Folder 	: Jonas/data/A3_gen.dta"
Date	: 2018-03-09

Creator		: Jonas Cederlof	(JC)
Description 	: 

Notes:

LATEST UPDATE: 	
*/
********************************************************************************

clear
set more			 off
cap   log close 		_all
	
*log using 	"../log/B1_prep_agedata.log" 	, replace 
use 		"$datapath/A3_gen_empmark.dta"


*Dropping dates prior to 2005:1 and after 2016m12 
keep if inrange(notdate_def,ym(2005,1),ym(2016,12))

*Dropping small notifications
drop if antvars<5

*If firm characteristics is missing post 2004 it is because the firm is non-existent
*in the data that year. The firm has gone bankcrupt and we know that the notification
*date is wrong.
sum atfirm if firm_avg_earnings==. & year>=2004
count if eventtime_def==0 & firm_avg_earnings==. & year>=2004
fastmax missingfirminRAMS_def, by(lopnr) name(temp)
drop if temp==1
drop temp

*Fixed effects
xtile xantvarsFE = antvars  if eventtime_def==0,n(10) 
fastmax xantvarsFE, name(antvarsFE) by(lopnr)

*Dropping where running variable age is missing
drop if age_def==.

*Dropping negative notification times
keep if inkom_dat<anstupp_dat

*Choosing bandwith of +/- 3 years
local band = 3
keep if age_def>=55-`band' & age_def<=55 + `band' & age_def!=55 
count if eventtime_def==0

*Drop Bankruptcy
*Label values of new variable
cap lab define orsak 	1 "Anna" 2 "Effe" 3 "Flve" 4 "L*ge" 5 "Nedl" 6 "Konk"
lab val varselorsak orsak
drop if varselorsak==5

*Keeping White collar workers
bys lopnr (date) : egen WC_at_not = max(WC==1 & eventtime_def==0 )
keep if WC_at_not==1 
drop WC_at_not


*Replace one missing pre earnings and 8 missing tenure
replace annual_ear_prenot_def=0 if annual_ear_prenot_def==.
replace tenureatnot=0 if tenureatnot==.


// Normalize control variables 
*===============================================================================
foreach var of   varlist annual_ear_prenot_def female immigrant tenureatnot educ0 educ1 educ2 educ3 educ4  {
	sum `var' 	
	gen C_`var' = `var' - r(mean)
}	
*
 


{ // Generate: Age +/- 55 control function  
********************************************************************************


*Generate normalized running variables and control functions
*Linear
gen 	runvar 	= age_def-55
gen  	treat  	= runvar>=0
gen 	inter 	= runvar*treat

*Quadratic
gen q_runvar = runvar^2
gen q_inter  = inter^2

*Cubic
gen c_runvar = runvar^3
gen c_inter  = inter^3


}
*


count
compress
save "$datapath/A3_age55data_WC.dta",replace




